﻿using System;
using System.Timers;
using Acme.Common.Infrastructure;
using Acme.MVVM.Scenarios.Events;
using Acme.Prism.Infrastructure;

namespace Acme.MVVM.Scenarios.Views
{
    public class SubscriberViewModel : ObservableObject
    {

        readonly Timer _timer;
        const string DefaultSubscriberMessage = "Awaiting message...";

        String _subscriberMessage;

        public String SubscriberMessage
        {
            get { return _subscriberMessage; }
            set
            {
                _subscriberMessage = value;
                RaisePropertyChanged("SubscriberMessage");
            }
        }

        public SubscriberViewModel(IEventAggregatorResolver<PubSubDemoEvent> subEvent)
        {
            _timer = new Timer(2500);
            _timer.Elapsed += (s, e) =>
            {
                this.SubscriberMessage = DefaultSubscriberMessage;
                _timer.Stop();
            };

            this.SubscriberMessage = DefaultSubscriberMessage;

            subEvent.Resolve().Subscribe(p =>
            {
                this.SubscriberMessage = p;
                _timer.Start();
            });
        }
    }
}
